package com.handpay.zztong.hp.accountcenter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.handpay.framework.ClientEngine;
import com.handpay.framework.SecureManager;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.accountcenter.model.checkphoneno.CheckPhoneNoModel;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.base.callback.BusinessEngineListener;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.bean.ResponseBean;
import com.handpay.zztong.hp.config.ZZTConstant;
import com.handpay.zztong.hp.login.model.sendphonecode.SendAuthCodeModel;
import com.handpay.zztong.hp.protocol.BaseProtocolKey;
import com.handpay.zztong.hp.ui.VerificationCodeButton;
import com.handpay.zztong.hp.utils.AccountUtils;

import java.util.Hashtable;

/**
 * 修改手机号码
 */
public class PhoneNoModifyActivity extends ZZTongActivity {

    private EditText original_mobile_number;
    private EditText messagecode;
    private VerificationCodeButton getmassagecode;
    private TextView surepassword;
    private String phoneNum;
//    private AuthCodeBill authCodeBill;

    @Override
    protected void onStart() {
        super.onStart();
        messagecode.setText("");
        surepassword.setText("");
        getmassagecode.cancelLocker();
    }

    @Override
    public ActionBar getActivityActionBar() {
        return new ActionBar(this.getString(R.string.Update_phone), true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.setContentView(R.layout.activity_phone_no_modify);
        super.onCreate(savedInstanceState);
        original_mobile_number = (EditText) findViewById(R.id.original_mobile_number);
        original_mobile_number.setCursorVisible(false);//
        original_mobile_number.setFocusable(false);//
        messagecode = (EditText) findViewById(R.id.messagecode);
        getmassagecode = (VerificationCodeButton) findViewById(R.id.getmassagecode);
        surepassword = (TextView) findViewById(R.id.surepassword);
        Button confirm = (Button) findViewById(R.id.btn_confirm);
        getmassagecode.setOnClickListener(this);
        confirm.setOnClickListener(this);
        Initialization();
//        authCodeBill = new AuthCodeBill(this);
    }

    protected boolean isShownTabs() {
        return false;
    }

    /***
     *
     */
    private void Initialization() {
        phoneNum = AccountUtils.getAccountPhone(this);
        if (AccountUtils.getAccountPhone(this) != null) {
            String temp = phoneNum;
            String start = temp.substring(0, 3);
            String end = temp.substring(temp.length() - 4);
            original_mobile_number.setText(start + " **** " + end);

        }
    }

    /***
     *
     */
    private void doSendPhoneCode() {

        if (!CommonUtils.isMobileNum(phoneNum)) {
            // this.showAlertDialog(this,
            // R.string.tip,R.string.input_right_phone_tip);
            Toast.makeText(this, R.string.input_right_phone_tip, Toast.LENGTH_SHORT).show();
            return;
        }
//        authCodeBill.sendAuthCode(ZZTConstant.AUTHCODE_ACTION_OLDPHONE, phoneNum);

        new SendAuthCodeModel().doSendAuthCodePost(this, ZZTConstant.AUTHCODE_ACTION_OLDPHONE, phoneNum, new BusinessEngineListener<ResponseBean>() {
            @Override
            public void callbackBusinessResponse(ResponseBean bean) {
                if (bean.getResponseCode().equals("0")) {
                    Toast.makeText(PhoneNoModifyActivity.this, "发送验证码成功！", Toast.LENGTH_SHORT).show();
                } else {
                    showAlertDialog(PhoneNoModifyActivity.this, getString(R.string.prompt), bean.getErrMessage());
                }
            }
        });
        getmassagecode.startLocker(-1);
    }

    private void checkPhoneNumber() {
        Hashtable<String, String> params = new Hashtable<>();
        params.put("phone", SecureManager.getInstance().des(phoneNum, 1, null));
        params.put("verifyCode", messagecode.getText().toString().trim());
        params.put("pwd", SecureManager.getInstance().des(surepassword.getText().toString().trim(), 1, null));
        params.put("action", ZZTConstant.AUTHCODE_ACTION_OLDPHONE);
        params.put(BaseProtocolKey.ACCOUNT, SecureManager.getInstance().des(AccountUtils.getAccountName(this), 1, null));
        params.put("channel", ClientEngine.CHANNEL);
//        doJsonPost(this, DO_zztCheckPhoneNum, ResponseBean.class, params);
        new CheckPhoneNoModel().doCheckPhoneNoPost(this, params, new BusinessEngineListener<ResponseBean>() {
            @Override
            public void callbackBusinessResponse(ResponseBean bean) {
                Intent intent = new Intent(PhoneNoModifyActivity.this, NewPhoneNoConfirmActivity.class);
                intent.putExtra("oldPhone", phoneNum);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private boolean IsControlValues() {
        if (!CommonUtils.isMobileNum(phoneNum)) {
            Toast.makeText(this, R.string.input_right_phone_tip, Toast.LENGTH_SHORT).show();
            return false;
        } else if (messagecode.getText().toString().trim().length() == 0) {
            Toast.makeText(this, R.string.input_right_message_code_tip, Toast.LENGTH_SHORT).show();
            return false;
        } else if (surepassword.getText().toString().trim().length() == 0) {
            Toast.makeText(this, R.string.input_login_password_please, Toast.LENGTH_SHORT).show();
            return false;
        } else if (!AccountUtils.isPWDLegal(surepassword.getText().toString(),6)) {
            Toast.makeText(this, R.string.illegal_input_password, Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.getmassagecode) {
            doSendPhoneCode();
        } else if (i == R.id.btn_confirm) {
            if (IsControlValues()) {
                checkPhoneNumber();
            }

        }
    }
}
